import { AudioClip, AudioSource, find, Node, NodePool } from "cc";

export class AudioManager extends et.Entity {
    private static instance: AudioManager;
    public static get Instance(): AudioManager {
        return this.instance;
    }
    public static set Instance(value: AudioManager) {
        this.instance = value;
    }
    public MusicSource: AudioSource;
    public EffectSource: AudioSource;
    public playEffect(clip: AudioClip) {
        this.EffectSource.playOneShot(clip);
    }
    public playMusic(clip: AudioClip, loop: boolean) {
        this.MusicSource.clip = clip;
        this.MusicSource.loop = loop;
        this.MusicSource.play();
    }
    private _MusicVolume = 1;
    public get MusicVolume(): number {
        return this._MusicVolume;
    }
    public set MusicVolume(v: number) {
        this._MusicVolume = v;
    }
    private _EffectVolume = 1;
    public get EffectVolume(): number {
        return this._EffectVolume;
    }
    public set EffectVolume(v: number) {
        this._EffectVolume = v;
    }
    public set Muted(mute: boolean) {
        this.EffectVolume = 0;
        this.MusicVolume = 0;
    }
}
@et.objectSystem(AudioManager)
class AudioManagerAwakeSystem extends et.AwakeSystem<AudioManager>{
    awake(self: AudioManager): void {
        AudioManager.Instance = self;
        let audioRoot = find("AudioRoot");
        if (!audioRoot) {
            audioRoot = new Node("AudioRoot");
            self.EffectSource = audioRoot.addComponent(AudioSource);
            self.MusicSource = audioRoot.addComponent(AudioSource);
        }
    }
}
@et.objectSystem(AudioManager)
class AudioManagerDestroySystem extends et.DestroySystem<AudioManager>{
    destroy(self: AudioManager): void {
        let audioRoot = cc.find("AudioRoot");
        if (audioRoot)
            audioRoot.destroy();
        self.EffectSource = null;
        self.MusicSource = null;
    }
}